package com.eystar.pdc.controller;

import com.alibaba.fastjson.JSONArray;
import com.eystar.ext.route.ControllerBind;
import com.eystar.pdc.aop.auth.AuthAnno;
import com.eystar.pdc.aop.auth.AuthAnno.AuthType;
import com.eystar.pdc.aop.log.LogAnno;
import com.eystar.pdc.aop.log.LogAnno.LogType;
import com.eystar.pdc.model.Role;
import com.eystar.pdc.model.RoleResource;

/**
 * 角色管理
 * @author Administrator
 *
 */
@ControllerBind(controllerKey="/roleResource", viewPath="/pdc")
@AuthAnno(type = AuthType.Super, res = "role")
public class RoleResourceController extends BaseController {
	
	/**
	 * 列表展示页面
	 * @throws Exception
	 */
	@AuthAnno(auth="look")
	@LogAnno(desc = "查看角色资源关键字列表", type = LogType.SEARCH)
	public void index() throws Exception{
		Role role = Role.dao.findById(getPara());
		setAttr("role_id", role.getStr("id"));
		setAttr("role_name", role.getStr("role_name"));
		render("role_resource_list.html");
	}
	
	/**
	 * 点击保存按钮
	 * @throws Exception
	 */
	@AuthAnno(auth="save")
	@LogAnno(desc = "角色分配资源关键字", type = LogType.SAVE)
	public void save() throws Exception{
		String[] signs = getParaValues("signs");
		String roleId = getPara("role_id");
		String resourceId = getPara("resource_id");
		try {
			RoleResource.dao.addRoleResouces(signs, roleId,resourceId);
			renderJson(ok());
		} catch (Exception e) {
			logger.error("添加出错！",e);
			renderJson(fail());
		}
	}
	
	/**
	 * 获取某个资源有哪些权限关键字可以分配，并且这些关键字是否被选中
	 * @throws Exception
	 */
	public void sign() throws Exception{
		JSONArray array = RoleResource.dao.findResourceAuthSigns(getPara(0), getPara(1));
		renderJson(array);
	}

}